package com.example.shushiyunsst.fragment.main.transaction.child.menu

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.MessageWrap
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.transaction.bean.SelectedMoneyBean
import com.example.shushiyunsst.fragment.main.transaction.bean.TransactionUIInvokeListener
import com.example.shushiyunsst.fragment.main.transaction.child.CHANG_ICON
import com.example.shushiyunsst.fragment.main.transaction.child.TransactionMenuFragment
import com.example.shushiyunsst.fragment.main.transaction.child.transfer_index
import com.senon.modularapp.util.JssSpannableString
import ikidou.reflect.TypeBuilder
import org.greenrobot.eventbus.EventBus
import java.lang.reflect.Type

class SelectedMoneyTypeFragment(override val title: String) :
    SimpleListFragment<SelectedMoneyBean>(), MenuLabInfo {

    companion object {
        fun newInstance(title: String): SelectedMoneyTypeFragment {
            val args = Bundle()
            val fragment = SelectedMoneyTypeFragment(title)
            fragment.arguments = args
            return fragment
        }

    }

    private var selectPos = 0

    private var transactionUIInvokeListener: TransactionUIInvokeListener? = null

    fun setTransactionUIInvokeListener(listener: TransactionUIInvokeListener?): SelectedMoneyTypeFragment {
        this.transactionUIInvokeListener = listener
        return this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        isEnableLoadMore = false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundColor(Color.TRANSPARENT)
        mSwipeRefreshLayout.setColorScheme(R.color.nav_bar_bg_color)
        mSwipeRefreshLayout.setProgressBackgroundColor(R.color.colorAccent)
    }

    override fun getItemLayout() = R.layout.selected_money_type_fragment_layout

    override fun convertItem(helper: JssBaseViewHolder?, item: SelectedMoneyBean?) {
        helper?.setText(
            R.id.iconTv,
            JssSpannableString(_mActivity, "${item?.tokenName}/${item?.tokenCurrency}")
                .first("${item?.tokenName}")
                .textColor(R.color.text_color_6)
                .size(16)
                .first("${item?.tokenCurrency}")
                .textColor(R.color.text_color_2)
                .size(12)
        )
            ?.setText(R.id.allValueTv, "${item?.tokenPrice}")

    }


    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(SelectedMoneyBean::class.java).endSubType().build()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        super.onItemClick(adapter, view, position)

        transactionUIInvokeListener?.toggleDrawerLayout()

        selectPos = position

        postIcon()
    }

    override fun netRequest() {

        RestClient.builder()
            .url(R.string.queryTransTokenList)
            .success {
                parseDate(it)
            }
            .error { _, _ ->
                onFailed()
            }.failure {
                onFailed()
            }.build().post()
    }

    override fun parseDate(data: String?) {

        val obj = JSON.parseObject(data)
        val list = obj["list"] ?: ""
        val array = JSON.parseArray(JSON.toJSONString(list), SelectedMoneyBean::class.java)
        loadListDate(array)
        postIcon()
    }

    private fun postIcon() {
        if (mAdapter.data.size > selectPos) {
            val item = mAdapter.getItem(selectPos)
            val instance = MessageWrap.getInstance(CHANG_ICON)

            val transIndexNode = transactionUIInvokeListener?.getTransIndexNode()
            transIndexNode?.iconType = item
            EventBus.getDefault().post(instance)

            val transferIndex = MessageWrap.getInstance(transfer_index)
            EventBus.getDefault().post(transferIndex)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        transactionUIInvokeListener = null
    }
}